package ar.edu.itba.pod.legajo47573;

import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;

import ar.edu.itba.node.Node;
import ar.edu.itba.node.api.NodeStatistics;
import ar.edu.itba.node.api.StatisticReports;
import ar.edu.itba.pod.agent.market.AgentState;
import ar.edu.itba.pod.agent.runner.Agent;

@SuppressWarnings("serial")
public class StatisticReportsImpl extends UnicastRemoteObject implements StatisticReports{

	private MyNode currentNode;

	public StatisticReportsImpl(MyNode currentNode) throws RemoteException{
		super();
		this.currentNode = currentNode;
		
		try {
			currentNode.getRegistry().bind(Node.STATISTIC_REPORTS, this);
		} catch (AlreadyBoundException e) {
			e.printStackTrace();
		}
	}

	/*Se llama remotamente para pedir info de un nodo.
	 * El nodo que recibe el pedido sólo debe crear un NodeStatistics con sus datos y devolverlo*/
	@Override
	public NodeStatistics getNodeStatistics() throws RemoteException {
		List<Agent> agents = new ArrayList<Agent>(currentNode.getSimulation().getAgentsRunning());
		int numberOfAgents = agents.size();
		List<AgentState> agentStates = new ArrayList<AgentState>();

		for(int i=0; i< numberOfAgents; i++){
			agentStates.add(agents.get(i).state());
		}

		return new NodeStatistics(numberOfAgents, agentStates);
	}

}
